(function() {
    angular.module('app').component('tableBrandShops', {
        templateUrl: 'app/components/table.list.component/table.brand.shops/table.brand.shops.html',
        controller: tableBrandShopsComponent,
        controllerAs: 'vm',
        bindings: {
            controllerFilter: '=',
            getType: '<', //类型
            monitorShopAcount: '='
        }
    });

    tableBrandShopsComponent.$inject = ['$rootScope', '$scope', 'tableBrandShopsService', '$loading', 'util'];

    function tableBrandShopsComponent($rootScope, $scope, tableBrandShopsService, $loading, util) {
        var vm = this;


        vm.filter = {
            begin: 0,
            // uid: '',
            brand: '',
            count: 110,
            sort: 'down',
            newPageNumber: 1,
            sortColumn: 'sell_money'
        };

        //table data
        vm.table = {
            // count: 0, 店铺数据没有分页
            shops: []
        };

        //排序
        vm.sort = sort; //function

        //分页
        vm.pageChanged = pageChanged; //function

        //获取店铺详细监控
        vm.getShopBasic = tableBrandShopsService.getShopBasic; //function

        //获取日监控
        vm.getMonitorData = tableBrandShopsService.getMonitorData; //function

        //获取品牌中的的店铺中的商品
        //发送广播分别到table.list.component和table.shop.prods
        vm.getBrandShopProds = getBrandShopProds; //function

        //monitor.controller
        vm.deleteCollection = deleteCollection; //function

        //品牌中的店铺数据
        var clearBrand = $rootScope.$on('formBrand_detail_EV', function(e, brandName) {
            vm.filter.count = 110;
            vm.filter.brand = brandName;

            // console.log("formBrand_detail_EV llllllllll");
            if(vm.getType == 'shop'){//防止覆盖调用
                init();
            }
            

        });


        active()

        /*------------------------------shop.controller部分-----------------------------------*/

        function active() {
            //shop.controller
            if (vm.controllerFilter) {
                // console.log('shop.controller部分');
                //     console.log(vm.controllerFilter);
                vm.table.count = 0;
                vm.filter.count = 11;

                //$watch shop.controller filte bind
                $scope.$watch('vm.controllerFilter', function(newValue, oldValue) {
                    // console.log('newValue');
                    // console.log(newValue);

                    shopControllerInit();
                }, true);
            }
            // if (vm.getType == 'shop') {
            //     vm.filter.count = 110;
            //     vm.filter.brand = brandName;

            //     init();
            // }

            //monitor.controller
            if (vm.getType == 'monitorShop') {
                monitorControllerInit();
            }

        }

        //分页 shop.controller 才有这个功能
        function pageChanged(newPageNumber) {
            // console.log(newPageNumber);
            vm.controllerFilter.newPageNumber = newPageNumber;
            vm.controllerFilter.begin = (vm.controllerFilter.newPageNumber - 1) * vm.controllerFilter.count;
            // shopControllerInit();
        }

        function init() {

            var json = angular.copy(vm.filter);
            //reset table
            $loading.start('tableBrandShopLoading');
            
            vm.table.shops.length = 0;
            //品牌店铺数据
            tableBrandShopsService.get(json).then(function(resp) {
                // console.log(resp);
                // vm.table.count = 0;
                
                // vm.table.count = resp.count / 10 * 11;
                vm.table.shops = resp.resultShops;

                angular.forEach(vm.table.shops, function(item, index) {

                    item = util.shopLevelFormat(item);


                });

                tableBrandShopsService.shopBasicInit(); //店铺详情的序号
                tableBrandShopsService.monitorDataInit(); //日监控所在的序号

                $loading.finish('tableBrandShopLoading');
            }, function(err) {
                // vm.table.count = 0;
                vm.table.products.length = 0;
                $loading.finish('tableBrandShopLoading');
            });
        }


        //shopControllerInit
        function shopControllerInit() {


            var json = $.extend(angular.copy(vm.filter), angular.copy(vm.controllerFilter));
            // console.log(json);

            $loading.start('tableBrandShopLoading');
            vm.table.shops.length = 0;

            //品牌店铺数据
            tableBrandShopsService.shopControllerGet(json).then(function(resp) {
                // console.log(resp);
                // vm.table.count = 0;
                // console.log(resp.count );
                vm.table.count = resp.count / 10 * 11;

                // console.log(resp.count / 10 * 11);

                // console.log(vm.table.count);

                vm.table.shops = resp.shopInfos;

                angular.forEach(vm.table.shops, function(item, index) {
                    item = util.shopLevelFormat(item);
                    //店铺等级

                });

                tableBrandShopsService.shopBasicInit(); //店铺详情的序号
                tableBrandShopsService.monitorDataInit(); //日监控所在的序号

                $loading.finish('tableBrandShopLoading');
            }, function(err) {
                // vm.table.count = 0;
                vm.table.products.length = 0;
                $loading.finish('tableBrandShopLoading');
            });
        }


        function sort(sortColumn) {


            if (vm.filter.sortColumn == sortColumn) {
                return;
            }
            vm.filter.sortColumn = sortColumn;
            
            // if(vm.getType == 'shop'){
            //     shopControllerInit();
            // }
           
        }


        function getBrandShopProds(shop) {
            //发送广播分别到table.list.component和table.shop.prods
            $rootScope.$emit('formBrandShop_detail_EV', shop.userId, shop.shopName);
        }



        /*------------------------------monitor.comtroller---------------------------*/

        //monitorControllerInit
        function monitorControllerInit() {
            $loading.start('tableBrandShopLoading');
            //品牌店铺数据
            tableBrandShopsService.monitorControllerGet().then(function(data) {
                // console.log(resp);
                // vm.table.count = 0;
                // 
                if($.isEmptyObject(data)){
                    vm.table.shops = [];

                    vm.monitorShopAcount = 0;
                    $loading.finish('tableBrandShopLoading');
                    return;
                }
                vm.table.shops.length = 0;

                vm.table.shops = data.collShops;


                //bind monitor.controller
                vm.monitorShopAcount = data.collShops.length;

                angular.forEach(vm.table.shops, function(item, index) {
                    item = util.shopLevelFormat(item);
                    //店铺等级

                });
                monitorShopCurIndex = -1; //店铺详情的序号
                monitorDataCurIndex = -1; //日监控所在的序号
                $loading.finish('tableBrandShopLoading');
            }, function(err) {
                // vm.table.count = 0;
                vm.table.products = [];
                $loading.finish('tableBrandShopLoading');
            });
        }

        function deleteCollection(shop) {

            tableBrandShopsService.deleteCollection(shop).then(function() {
                var index = vm.table.shops.indexOf(shop);

                vm.table.shops.splice(index, 1);
            });
        }




        $scope.$on('$destroy', function() {

            clearBrand();
        });
    }
})();
